package com.hiv.mapper;

import com.hiv.common.param.page.PageQueryRoleParam;
import com.hiv.mapper.domain.Role;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * role
 */
@Mapper
@Repository
public interface RoleMapper {
    @Insert({"insert into role(name, `desc`, platform, wechat_role_type, super_admin_id, hospital_id, operator) values (#{name}, #{desc}, #{platform}, #{wechatRoleType}, #{superAdminId}, #{hospitalId}, #{operator})"})
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    long insert(Role role);
    @Insert({"update role set name = #{role.name}, `desc` = #{role.desc}, platform = #{role.platform}, wechat_role_type=#{role.wechatRoleType}, operator = #{role.operator}, super_admin_id=#{role.superAdminId}, hospital_id=#{role.hospitalId} where id = #{role.id} limit 1"})
    boolean update(@Param("role") Role role);
    @Delete("delete from role where id = #{id} limit 1")
    void delete(@Param("id") long id);

    @Select({"<script>",
            "   select * from role",
            "   <where>",
            "       id!=1",
            "       <if test=\"param.superAdminId!=null\">and super_admin_id=#{param.superAdminId}</if>",
            "   </where>",
            "</script>"})
    List<Role> listAll(@Param("param") Role param);

    @Select({"select * from role where name = #{name} limit 1"})
    Role findByName(@Param("name") String name);

    @Select({"select * from role where name = #{name} and hospital_id=#{hospitalId} limit 1"})
    Role findByNameAndHospital(@Param("name") String name, @Param("hospitalId") Long hospitalId);

    @Select("select r.* from role r inner join admin_role ar on r.id=ar.role_id where ar.admin_id=#{adminId} limit 1")
    Role findByAdminId(Long adminId);

    @Select("select count(1) from role r inner join admin_role ar on r.id=ar.role_id where ar.admin_id=#{adminId} and id in (7, 8)")
    Integer countKgOrJyyByAdminId(Long adminId);

    @Select({"<script>",
            " select count(1) from role ",
            "   <where>",
            "       id!=1",//排除超级管理员角色
            "       <if test=\"param.roleId == 2\">and id!=2</if> ",//当前是医院管理员登录,排除医院管理员
            "       <if test=\"param.hospitalId!=null\">and hospital_id=#{param.hospitalId}</if> ",
            "       <if test=\"param.name != '' and param.name != null\">and name like CONCAT('%',#{param.name},'%')</if> ",
            "   </where>",
            "</script>"})
    long pageCount(@Param("param") PageQueryRoleParam param);

    @Select({"<script>",
            "   select * from role ",
            "   <where>",
            "       id!=1",//排除超级管理员角色
            "       <if test=\"param.roleId == 2\">and id!=2</if> ",//当前是医院管理员登录,排除医院管理员
            "       <if test=\"param.hospitalId!=null\">and hospital_id=#{param.hospitalId}</if> ",
            "       <if test=\"param.name != '' and param.name != null\">and name like CONCAT('%',#{param.name},'%')</if> ",
            "   </where>",
            "   limit #{limit} offset #{offset}",
            "</script>"})
    List<Role> pageQuery(@Param("param") PageQueryRoleParam param, @Param("limit") int limit, @Param("offset") long offset);

}